<?php
class Aaron_Siblings_Block_Product_Siblings extends Mage_Catalog_Block_Product_Abstract {
	protected $_itemLimit = 40;
	protected $_cacheeLifeTime = 60;
	
	public function getCacheLifetime()
	{
		return $this->_cacheeLifeTime;
	}
	
	public function getCacheKey()
    {
		$_taxCalculator = Mage::getModel('tax/calculation');
		$_customer = Mage::getSingleton('customer/session')->getCustomer();
		$_product = $this->getProduct();
        return 'ProductSiblings_'.
			/* Create differnet caches for ...
			 * ... for different products */
			$_product->getId().'_'.
			/* ... for different stores */
			Mage::App()->getStore()->getCode().'_'.
			/* ... for different customer groups */
			$_customer->getGroupId().'_'.
			/* ... for different tax classes (related to customer and product) */
			$_taxCalculator->getRate(
				$_taxCalculator
					->getRateRequest()
					->setProductClassId($_product->getTaxClassId()
				)
			);
	}


    public function getCacheTags()
    {
		return array(
			Mage_Catalog_Model_Product::CACHE_TAG,
			Mage_Catalog_Model_Product::CACHE_TAG."_".$this->getProduct()->getId().'_siblings'
		);

	}
	
	public function getSiblingsProducts(){
		$_cat = $this->getCurrentCategory();
		$products = Mage::getResourceModel('reports/product_collection');
		$products->getSelect()->order('rand()');
		$products->addUrlRewrite()
		->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
		->addAttributeToSelect('*')
		->addCategoryFilter($_cat)
		->setPageSize($this->_itemLimit);
		return $products;
	 }
	 
    /**
     * Enter description here...
     *
     * @return Mage_Catalog_Model_Category
     */
	public function getCurrentCategory()
    {
        $category = $this->getData('current_category');
        if (is_null($category)) {
            if ($category = Mage::registry('current_category')) {
                $this->setData('current_category', $category);
            } elseif ($product = $this->getCurrentProduct()) {
				$categoryIds = $product->getCategoryIds();
				$category = Mage::getModel('catalog/category')->load($categoryIds[0]);
                $this->setData('current_category', $category);
			} else {
                $category = Mage::getModel('catalog/category')->load($this->getCurrentStore()->getRootCategoryId());
                $this->setData('current_category', $category);
            }
        }

        return $category;
    }
	
	public function getCurrentProduct()
    {
        if(!Mage::registry('product'))
			return false;
        return Mage::registry('product');
    }
	
	public function setItemLimit($limit){		
        if (intval($limit) > 0) {
            $this->_itemLimit = intval($limit);
        }
        return $this;
    }

}